package cm.mefobe.android.telecomcost.view;

import java.security.InvalidParameterException;

import cm.mefobe.android.telecomcost.entities.WeeklyThreshold;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TableRow;

public class WeeklyThresholdEditView extends TableRow {

	private Context _context;
	private AmountView _lblAmount;
	private WeekView weekView;
	private WeeklyThreshold _threshold;

	public void setWeeklyThreshold(WeeklyThreshold threshold) {
		this._threshold = threshold;
		setThreshold();
	}

	public void setAmount(double amount) {
		if (_lblAmount == null) {
			setWeeklyThresholdView();
		} 
		
		_lblAmount.setAmount(amount);
		_threshold.setAmount(amount);
	}
	
	public double getAmount()
	{
		if (_lblAmount != null) {
			return _lblAmount.getAmount();
		} else {
			throw new InvalidParameterException("cannot getAmount .....lblAmount is null");
		}
	}

	public WeeklyThreshold getWeeklyThreshold() {
		return _threshold;
	}

	public WeeklyThresholdEditView(Context context, WeeklyThreshold threshold) {
		super(context);
		Log.i("WeeklyThresholdView", "creating the WeeklyThresholdView");
		_context = context;
		setWeeklyThreshold(threshold);
	}

	private void setWeeklyThresholdView() {
		if (isInEditMode()) {
			Log.i("WeeklyThresholdView", "isInEditMode");
		} else {
			if (_lblAmount == null) {
				Log.i("WeeklyThresholdView", "adding views...");

				_lblAmount = new AmountView(_context);
				_lblAmount.setLabel("");
				weekView = new WeekView(_context);	
				
				_lblAmount.getEditText().setOnFocusChangeListener(new OnFocusChangeListener() {
					
					public void onFocusChange(View v, boolean hasFocus) {
						Log.i("WeeklyThresholdView", "_lblAmount onFocusChange...hasFocus " + hasFocus);
						if(!hasFocus)
						{
							Log.i("WeeklyThresholdView", "updating amount");
							setAmount(Double.parseDouble(_lblAmount.getEditText().getText().toString()));
						}
					}
				});
				
				addView(weekView);
				addView(_lblAmount);
				setPadding(1,2,1,2);
			} else {
				Log.i("WeeklyThresholdView",
						"WeeklyThresholdView already set...");
			}
		}
	}

	private void setThreshold() {
		if (_lblAmount == null) {	
			setWeeklyThresholdView();
		}
		
		_lblAmount.setAmount(_threshold.getAmount());
		weekView.setWeek(_threshold.getWeek());
	}

	public WeeklyThresholdEditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("WeeklyThresholdView",
				"creating the WeeklyThresholdView with AttributeSet");

		_context = context;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("WeeklyThresholdView", "onLayout");
		super.onLayout(changed, l, t, r, b);
		setWeeklyThresholdView();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("WeeklyThresholdView", "onAttachedToWindow");
		super.onAttachedToWindow();
		setWeeklyThresholdView();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("WeeklyThresholdView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("WeeklyThresholdView", "onFinishInflate");
		super.onFinishInflate();
		setWeeklyThresholdView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("WeeklyThresholdView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public WeekView getWeekView() {
		if (weekView == null) {
			throw new UnsupportedOperationException(
					"WeeklyThresholdView weekView is not set");
		}
		return weekView;
	}
}